#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define sz(s) (ll)(s.size())
#define ios ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
#define mod 1000000007
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
inline int add(int A, int B) { if(A + B >= mod) return A + B - mod; return A + B; }
inline int sub(int A, int B) { if(A - B < 0) return A - B + mod; return A - B; }
inline int mul(int A, int B) { return (ll)A * B % mod; }
inline int pot(int A, int B){
int ret = 1, bs = A;
for(; B ; B >>= 1){
if(B & 1) ret = mul(ret, bs);
bs = mul(bs, bs);
}
return ret%mod;
}*/
#define maxn 100005
ll n;
ll a[maxn];
ll inc(ll l){
while(l<n-1&&a[l+1]>a[l])l++;
return l;
}
ll dec(ll r){
while(r>0&&a[r]<a[r-1])r--;
return r;
}
void tc(){
cin >> n;
for(int i = 0;i<n;i++) cin >> a[i];
ll l=0,r=n-1;
l=inc(l);
r=dec(r);
ll ans=0;
while(l<r){
if(a[l]>a[r]){
ans+=a[r]+1-a[r-1];
a[r-1]=a[r]+1;
r=dec(r);
}
else{
ans+=a[l]+1-a[l+1];
a[l+1]=a[l]+1;
l=inc(l);
}
}
cout<<ans<<endl;
}
int main(){
ios
int t; t = 1;
//cin >> t;
while(t--){
tc();
}
return 0;
}
129. Sum Root to Leaf Numbers | 120. Triangle |
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |
1092. Shortest Common Supersequence | 1044. Longest Duplicate Substring |
1032. Stream of Characters | 987. Vertical Order Traversal of a Binary Tree |
952. Largest Component Size by Common Factor | 212. Word Search II |
174. Dungeon Game | 127. Word Ladder |
123. Best Time to Buy and Sell Stock III | 85. Maximal Rectangle |
84. Largest Rectangle in Histogram | 60. Permutation Sequence |
42. Trapping Rain Water | 32. Longest Valid Parentheses |